/*
 * $Id: DeleteHorarioPage.java 634 2006-03-26 18:28:10 -0800 (Sun, 26 Mar 2006) ivaynberg $
 * $Revision: 634 $
 * $Date: 2006-03-26 18:28:10 -0800 (Sun, 26 Mar 2006) $
 *
 * ==============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package at.horario.web.pages;

import at.horario.model.Horario;
import at.horario.service.HorarioManager;
import org.apache.commons.lang.ObjectUtils;
import org.apache.wicket.Page;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.collections.MicroMap;
import org.apache.wicket.util.string.interpolator.MapVariableInterpolator;

/**
 * Delete the Horario.
 *
 * @author AT
 */
@AuthorizeInstantiation({"user", "admin"})
public class DeleteHorarioPage extends PageTemplate {
    private final Page backPage;
    @SpringBean(name = "horarioManager")
    private HorarioManager horarioManager;

    /**
     * Constructor. Display the summary (names) before asking for confirmation.
     * Note that if you don't need the page to be bookmarkable, you can use
     * whatever constructor you need, such as is done here.
     *
     * @param backPage The page that the user was on before coming here
     * @param horario  Model that contains the horario to be deleted
     */
    public DeleteHorarioPage(Page backPage, IModel horario) {
        this.backPage = backPage;
        setModel(horario);
        add(new Label("horarioId", ObjectUtils.toString(getHorario().getHorarioId())));
        addConfimButton();
        addCancelButton();
    }

    private void addConfimButton() {
        /*
           * notice in mark-up this link is attached to <input type='button'/>
           * tag, the link is smart enough to know to generate an onclick instead
           * of href
           */
        add(new Link("confirm", new ResourceModel("confirm")) {
            @Override
            public void onClick() {
                final Horario deleted = getHorario();
                horarioManager.remove(deleted.getId());
                String msg = MapVariableInterpolator.interpolate(getLocalizer()
                        .getString("status.deleted", this), new MicroMap(
                        "horarioId", deleted.getHorarioId()));
                getSession().info(msg);
                setResponsePage(DeleteHorarioPage.this.backPage);
            }
        });
    }

    private void addCancelButton() {
        add(new Link("cancel", new ResourceModel("cancel")) {
            @Override
            public void onClick() {
                String msg = MapVariableInterpolator.interpolate(getLocalizer()
                        .getString("status.cancelled", this), new MicroMap(
                        "horarioId", getHorario().getHorarioId()));
                getSession().info(msg);
                setResponsePage(DeleteHorarioPage.this.backPage);
            }
        });
    }

    /**
     * Type-safe way to retrieve the horario from the page's model
     *
     * @return <code>Horario</code> instance stored in model
     */
    private Horario getHorario() {
        return (Horario) getModelObject();
    }

}
